package com.demo.schedule.regular;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.demo.entity.DvtDiagnose;
import com.demo.entity.VtePreventive;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;

@Slf4j
public class RegularMindUtil {

    /**
     * vte低风险、极低风险，每周提醒一次
     *
     * @param vtePreventive
     * @return
     */
    public static Long getVteRiskRankLevel1Or4(VtePreventive vtePreventive) {
        Date riskConfirmTime = vtePreventive.getRiskConfirmTime();
        Date nowDate = DateUtil.date(System.currentTimeMillis());
        long betweenDay = DateUtil.betweenDay(riskConfirmTime, nowDate, true);
        Date todaySignDate = DateUtil.offsetDay(riskConfirmTime, Integer.parseInt(String.valueOf(betweenDay)));//将签字日期偏移到今天

        log.info("getRiskRankLevel1Or4-签字日期 signDate={}", DateUtil.format(riskConfirmTime, DatePattern.NORM_DATETIME_PATTERN));
        log.info("getRiskRankLevel1Or4-当前日期 nowDate={}", DateUtil.format(nowDate, DatePattern.NORM_DATETIME_PATTERN));

        // 获得指定日期是星期几，1表示周一, ... 7表示周末
        int signWeek = DateUtil.dayOfWeek(riskConfirmTime) - 1 == 0 ? 7 : DateUtil.dayOfWeek(riskConfirmTime) - 1;
        int nowWeek = DateUtil.dayOfWeek(nowDate) - 1 == 0 ? 7 : DateUtil.dayOfWeek(nowDate) - 1;

        Date remindDate = null;
        if (signWeek > nowWeek) {
            remindDate = DateUtil.offsetDay(todaySignDate, signWeek - nowWeek);//这周提醒
        } else if (signWeek < nowWeek) {
            remindDate = DateUtil.offsetDay(todaySignDate, 7 - (nowWeek - signWeek));//下周提醒
        } else {
            // 处理都是同一个周几的情况(是否今天提醒)，比对小时
            if (todaySignDate.getTime() > nowDate.getTime()) {
                remindDate = todaySignDate;//今天提醒
            } else if (todaySignDate.getTime() < nowDate.getTime()) {
                remindDate = DateUtil.offsetWeek(todaySignDate, 1);//下周提醒
            } else {
                log.info("getRiskRankLevel1Or4-签字时间分钟和当前时间分钟相等，定时提醒任务默认一分钟后开始执行任务进行提醒");
                return 1L;
            }
        }
        log.info("getRiskRankLevel1Or4-提醒日期 remindDate={}", DateUtil.format(remindDate, DatePattern.NORM_DATETIME_PATTERN));
        long betweenMinute = DateUtil.between(remindDate, nowDate, DateUnit.MINUTE);
        return betweenMinute;
    }

    /**
     * vte高危，每天提醒
     *
     * @param vtePreventive
     * @return
     */
    public static Long getVteRiskRankLevel2(VtePreventive vtePreventive) {
        Date riskConfirmTime = vtePreventive.getRiskConfirmTime();
        Date nowDate = DateUtil.date(System.currentTimeMillis());
        long betweenDay = DateUtil.betweenDay(riskConfirmTime, nowDate, true);
        Date todaySignDate = DateUtil.offsetDay(riskConfirmTime, Integer.parseInt(String.valueOf(betweenDay)));//将签字日期偏移到今天,用于换算提醒时间

        log.info("getRiskRankLevel2-签字日期 signDate={}", DateUtil.format(riskConfirmTime, DatePattern.NORM_DATETIME_PATTERN));
        log.info("getRiskRankLevel2-当前日期 nowDate={}", DateUtil.format(nowDate, DatePattern.NORM_DATETIME_PATTERN));

        Date remindDate = null;
        if (todaySignDate.getTime() > nowDate.getTime()) {
            remindDate = todaySignDate;//今天提醒
        } else if (todaySignDate.getTime() < nowDate.getTime()) {
            remindDate = DateUtil.offsetDay(todaySignDate, 1);//明天提醒
        } else {
            log.info("getRiskRankLevel2-签字时间分钟和当前时间分钟相等，定时提醒任务默认一分钟后开始执行任务进行提醒");
            return 1L;
        }
        log.info("getRiskRankLevel2-提醒日期 remindDate={}", DateUtil.format(remindDate, DatePattern.NORM_DATETIME_PATTERN));
        long betweenMinute = DateUtil.between(remindDate, nowDate, DateUnit.MINUTE);
        return betweenMinute;
    }

    /**
     * vte中危, 每周提醒两次（周一、周四：周二、周五；周三、周六；周日：周三、周六）
     * 3 天一个周期
     *
     * @param vtePreventive
     * @return
     */
    public static Long getVteRiskRankLevel3(VtePreventive vtePreventive) {
        int rate = 3;//周期
        Date riskConfirmTime = vtePreventive.getRiskConfirmTime();
        Date nowDate = DateUtil.date(System.currentTimeMillis());

        log.info("getRiskRankLevel3-签字日期 signDate={}", DateUtil.format(riskConfirmTime, DatePattern.NORM_DATETIME_PATTERN));
        log.info("getRiskRankLevel3-当前日期 nowDate={}", DateUtil.format(nowDate, DatePattern.NORM_DATETIME_PATTERN));

        long betweenDay = DateUtil.betweenDay(riskConfirmTime, nowDate, true);
        //将签字日期偏移到今天
        Date todaySignDate = DateUtil.offsetDay(riskConfirmTime, Integer.parseInt(String.valueOf(betweenDay)));

        // 获得指定日期是星期几，1表示周一, ... 7表示周末
        // 签字时是周几，sign_time
        int signWeek = DateUtil.dayOfWeek(riskConfirmTime) - 1 == 0 ? 7 : DateUtil.dayOfWeek(riskConfirmTime) - 1;
        // 今天是周几
        int nowWeek = DateUtil.dayOfWeek(nowDate) - 1 == 0 ? 7 : DateUtil.dayOfWeek(nowDate) - 1;

        // 最终要进行提醒的日期(目标结果)
        Date remindDate = null;
        if (signWeek == nowWeek) {
            log.info("case  signWeek = nowWeek <==> {} = {}", signWeek, nowWeek);
            if (signWeek != 7) {//不是周末 signWeek=nowWeek => [1,2,3,4,5,6]
                if (todaySignDate.getTime() > nowDate.getTime()) {
                    remindDate = todaySignDate;//今天提醒
                } else if (todaySignDate.getTime() < nowDate.getTime()) {
                    int offsetDay = rate;//默认不跨周一个周期后提醒
                    if (nowWeek == 4 || nowWeek == 5 || nowWeek == 6) {//跨周的情况就 4 天后提醒
                        offsetDay = offsetDay + 1;
                    }
                    remindDate = DateUtil.offsetDay(todaySignDate, offsetDay);//三天后在提醒, 如果跨周了则四天后提醒
                } else {
                    log.info("getRiskRankLevel3-[signWeek == nowWeek]，定时提醒任务默认一分钟后开始执行任务进行提醒");
                    return 1L;
                }
            } else if (signWeek == 7) {//签字日期是周末 signWeek=nowWeek => 7
                remindDate = DateUtil.offsetDay(todaySignDate, rate);//三天后在提醒
            }
        }

        if (signWeek > nowWeek) {  //signWeek=[2,3,4,5,6,7] nowWeek=[1,2,3,4,5,6]
            log.info("case  signWeek > nowWeek <==> {} > {}", signWeek, nowWeek);
            if (signWeek != 7) {//不是周末 signWeek=[2,3,4,5,6] nowWeek=[1,2,3,4,5]
                int diffDay = signWeek - nowWeek;
                if (diffDay == rate) {//刚好等于三天，一个周期
                    if (todaySignDate.getTime() > nowDate.getTime()) {
                        remindDate = todaySignDate;//今天提醒
                    } else if (todaySignDate.getTime() < nowDate.getTime()) {
                        remindDate = DateUtil.offsetDay(todaySignDate, rate);//三天后在提醒
                    } else {
                        log.info("getRiskRankLevel3-[signWeek > nowWeek]，定时提醒任务默认一分钟后开始执行任务进行提醒");
                        return 1L;
                    }
                } else if (diffDay > rate) {
                    remindDate = DateUtil.offsetDay(todaySignDate, signWeek - rate - nowWeek);//(2*rate - nowWeek)天后提醒
                } else if (diffDay < rate) {
                    remindDate = DateUtil.offsetDay(todaySignDate, signWeek - nowWeek);//(rate - nowWeek)天后提醒
                }
            } else if (signWeek == 7) {//签字日期是周末, nowWeek 范围[1,2,3,4,5,6]
                if (nowWeek == 1 || nowWeek == 4) {
                    remindDate = DateUtil.offsetDay(todaySignDate, 2);
                } else if (nowWeek == 2 || nowWeek == 5) {
                    remindDate = DateUtil.offsetDay(todaySignDate, 1);
                } else if (nowWeek == 3 || nowWeek == 6) {
                    if (todaySignDate.getTime() > nowDate.getTime()) {
                        remindDate = todaySignDate;//今天提醒
                    } else if (todaySignDate.getTime() < nowDate.getTime()) {
                        remindDate = DateUtil.offsetDay(todaySignDate, rate);//三天后在提醒
                    } else {
                        log.info("getRiskRankLevel3-[signWeek > nowWeek]，定时提醒任务默认一分钟后开始执行任务进行提醒");
                        return 1L;
                    }
                }
            }
        }

        if (signWeek < nowWeek) {//signWeek=[1,2,3,4,5,6] nowWeek=[2,3,4,5,6,7]
            log.info("case  signWeek < nowWeek <==> {} < {}", signWeek, nowWeek);
            if (nowWeek != 7) {//今天不是周末 signWeek=[1,2,3,4,5] nowWeek=[2,3,4,5,6]
                int diffDay = nowWeek - signWeek;
                if (diffDay > rate) {//signWeek=[1,2] nowWeek=[5,6]
                    remindDate = DateUtil.offsetDay(todaySignDate, rate + 1);//三天后在提醒
                } else if (diffDay < rate) {
                    if (nowWeek == 4 || nowWeek == 5 || nowWeek == 6) {
                        remindDate = DateUtil.offsetDay(todaySignDate, signWeek + rate + 1 - nowWeek);//三天后在提醒
                    } else {
                        remindDate = DateUtil.offsetDay(todaySignDate, signWeek + rate - nowWeek);//三天后在提醒
                    }
                } else {//刚好等于三天，一个周期 signWeek=[1,2,3] nowWeek=[4,5,6]
                    if (todaySignDate.getTime() > nowDate.getTime()) {
                        remindDate = todaySignDate;//今天提醒
                    } else if (todaySignDate.getTime() < nowDate.getTime()) {
                        remindDate = DateUtil.offsetDay(todaySignDate, rate + 1);//4天后在提醒
                    } else {
                        log.info("getRiskRankLevel3-[signWeek < nowWeek]，定时提醒任务默认一分钟后开始执行任务进行提醒");
                        return 1L;
                    }
                }
            } else if (nowWeek == 7) {//今天是周末 signWeek=[1,2,3,4,5,6] nowWeek=[7]
                if (signWeek == 1 || signWeek == 4) {
                    remindDate = DateUtil.offsetDay(todaySignDate, 1);//1天后在提醒
                } else if (signWeek == 2 || signWeek == 5) {
                    remindDate = DateUtil.offsetDay(todaySignDate, 2);//2天后在提醒
                } else if (signWeek == 3 || signWeek == 6) {
                    remindDate = DateUtil.offsetDay(todaySignDate, 3);//3天后在提醒
                }
            }
        }
        log.info("getRiskRankLevel3-提醒日期 remindDate={}", DateUtil.format(remindDate, DatePattern.NORM_DATETIME_PATTERN));
        long betweenMinute = DateUtil.between(remindDate, nowDate, DateUnit.MINUTE);
        return betweenMinute;
    }

    /**
     * dvt 一周提醒
     *
     * @param dvtDiagnose
     * @return
     */
    public static Long getDvtRemind(DvtDiagnose dvtDiagnose) {
        Date signDate = dvtDiagnose.getSignTime();
        Date nowDate = DateUtil.date(System.currentTimeMillis());
        long betweenDay = DateUtil.betweenDay(signDate, nowDate, true);
        Date todaySignDate = DateUtil.offsetDay(signDate, Integer.parseInt(String.valueOf(betweenDay)));//将签字日期偏移到今天

        log.info("getDvtRemind-签字日期 signDate={}", DateUtil.format(signDate, DatePattern.NORM_DATETIME_PATTERN));
        log.info("getDvtRemind-当前日期 nowDate={}", DateUtil.format(nowDate, DatePattern.NORM_DATETIME_PATTERN));

        // 获得指定日期是星期几，1表示周一, ... 7表示周末
        int signWeek = DateUtil.dayOfWeek(signDate) - 1 == 0 ? 7 : DateUtil.dayOfWeek(signDate) - 1;
        int nowWeek = DateUtil.dayOfWeek(nowDate) - 1 == 0 ? 7 : DateUtil.dayOfWeek(nowDate) - 1;

        Date remindDate = null;
        if (signWeek > nowWeek) {
            remindDate = DateUtil.offsetDay(todaySignDate, signWeek - nowWeek);//这周提醒
        } else if (signWeek < nowWeek) {
            remindDate = DateUtil.offsetDay(todaySignDate, 7 - (nowWeek - signWeek));//下周提醒
        } else {
            // 处理都是同一个周几的情况(是否今天提醒)，比对小时
            if (todaySignDate.getTime() > nowDate.getTime()) {
                remindDate = todaySignDate;//今天提醒
            } else if (todaySignDate.getTime() < nowDate.getTime()) {
                remindDate = DateUtil.offsetWeek(todaySignDate, 1);//下周提醒
            } else {
                log.info("getRiskRankLevel1Or4-签字时间分钟和当前时间分钟相等，定时提醒任务默认一分钟后开始执行任务进行提醒");
                return 1L;
            }
        }
        log.info("getDvtRemind-提醒日期 remindDate={}", DateUtil.format(remindDate, DatePattern.NORM_DATETIME_PATTERN));
        long betweenMinute = DateUtil.between(remindDate, nowDate, DateUnit.MINUTE);
        return betweenMinute;
    }
}
